package com.zmj.sy.mom.srv.aps.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.zmj.sy.mom.srv.aps.bean.entity.mes.WorkDetailTask;
import com.zmj.sy.mom.srv.aps.bean.vo.apsworkdetail.TomorrowScheduleListVo;
import com.zmj.sy.mom.srv.aps.bean.vo.workshop.WaitWorkAsseVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface WorkDetailTaskMapper extends SyMapper<WorkDetailTask> {

    @Select(" select max(unique_code)  " +
            " from tb_aps_work_detail awd " +
            " left join tb_order o on awd.order_id = o.id " +
            " ${ew.customSqlSegment}")
    String selectMaxCode(@Param(Constants.WRAPPER) QueryWrapper<WorkDetailTask> wrapper);

    @Select("delete from tb_aps_order ${ew.customSqlSegment} ")
    void deleteByMaterial(@Param(Constants.WRAPPER) LambdaQueryWrapper<WorkDetailTask> query);

    @Select("DELETE FROM tb_mes_work_detail_task WHERE id = #{id}")
    void deleteMesNest(@Param("id") Integer id);


    @Select("DELETE FROM tb_mes_work_detail_task WHERE aps_work_detail_id = #{apsWorkDetailId}")
    void deleteByWorkDetailId(@Param("apsWorkDetailId") Integer apsWorkDetailId);

    @Select("DELETE FROM tb_mes_work_detail_task WHERE mater_code = #{plateCode}")
    void deleteMesNestWithCode(@Param("plateCode") String plateCode);

    @Select("SELECT " +
            "tm.* , " +
            "td.order_id as orderId, " +
            "tr.construct_no as constructNo " +
            "FROM " +
            "tb_mes_work_detail_task tm " +
            "LEFT JOIN tb_aps_work_detail td ON tm.aps_work_detail_id = td.id " +
            "LEFT JOIN tb_order tr ON td.order_id = tr.id " +
            "${ew.customSqlSegment}")
    List<WorkDetailTask> getTasklist(@Param(Constants.WRAPPER) QueryWrapper<WorkDetailTask> objectQueryWrapper);

    @Select(" SELECT ob.plm_id AS assemblyNumber, zob.plm_id plmId , max(zob.part_count) partCount, tor.id orderId, tor.construct_no constructNo " +
            " FROM tb_aps_work_detail awd " +
            " LEFT JOIN tb_order_bom ob ON awd.mater_no = ob.plm_id  AND ob.order_id = awd.order_id " +
            " LEFT JOIN tb_order_bom zob ON ob.id = zob.pid  AND zob.deleted = 0 " +
            " LEFT JOIN tb_order tor ON awd.order_id = tor.id  AND tor.order_type IN (1, 3, 4) AND tor.deleted = 0 AND tor.order_status = 8 " +
            " WHERE awd.STATUS IN (2, 3)  AND group_id = #{groupId} and tor.id is not null and awd.plm_id in (${assStr})  " +
            " GROUP BY ob.plm_id, zob.plm_id, tor.id, tor.construct_no " +
            " order by tor.construct_no ")
    List<WaitWorkAsseVo> getAllAsseWorks1(@Param("assStr") String assStr, @Param("groupId") Integer groupId);

    @Select(" select awd.order_id orderId, awd.plm_id partNo, sum(task.finish_count ) finishCount" +
            " from tb_mes_work_detail_task task" +
            " left join tb_aps_work_detail awd on task.aps_work_detail_id = awd.id " +
            " where task.type = 2  " +
            " and task.group_code = #{workPlaceName} " +
            " AND task.actual_end_date_time >=  #{startDate} " +
            " AND task.actual_end_date_time < #{endDate} " +
            " AND task.status  = 3 " +
            " group by awd.order_id, awd.plm_id  ")
    List<TomorrowScheduleListVo> findFinish(@Param("workPlaceName") String workPlaceName, @Param("startDate") String startDate, @Param("endDate") String endDate);

}
